#include "playscene.h"
#include<QMenuBar>
#include<QMenu>
#include<QAction>
#include<QPainter>
#include<mypushbutton.h>
#include<QTimer>
#include<QLabel>
#include<mycoin.h>
#include<dataconfig.h>
//PlayScene::PlayScene(QWidget *parent)
//    : QMainWindow{parent}
//{



//}

PlayScene::PlayScene(int levalIndex)
{
    //qDebug() << "当前关卡为"<< index;
    this->levalIndex = levalIndex;
    //设置窗口固定大小
    this->setFixedSize(320,588);
    //设置图标
    this->setWindowIcon(QPixmap(":/res/Coin0001.png"));
    //设置标题
    this->setWindowTitle("翻金币");

    //创建菜单栏
    QMenuBar * bar = this->menuBar();
    this->setMenuBar(bar);
    //创建开始菜单
    QMenu * startMenu = bar->addMenu("开始");
    //创建按钮菜单项
    QAction * quitAction = startMenu->addAction("退出");
    //点击退出 退出游戏
    connect(quitAction,&QAction::triggered,[=](){this->close();});

    //qDebug() << this->levalIndex;

    //返回按钮
    MyPushButton * closeBtn = new MyPushButton(":/res/BackButton.png",":/res/BackButtonSelected.png");
    closeBtn->setParent(this);
    closeBtn->move(this->width()-closeBtn->width(),this->height()-closeBtn->height());
    //返回按钮功能实现
    connect(closeBtn,&MyPushButton::clicked,[=](){
        QTimer::singleShot(300, this,[=](){
            this->hide();
            //触发自定义信号，关闭自身，该信号写到 signals下做声明
            emit this->playSceneBack();
        });
    });


    //当前关卡标题
    QLabel * label = new QLabel;
    label->setParent(this);
    QFont font;
    font.setFamily("华文新魏");
    font.setPointSize(20);
    label->setFont(font);
    QString str = QString("Leavel: %1").arg(this->levalIndex);
    label->setText(str);
    label->setGeometry(QRect(30, this->height() - 50,120, 50)); //设置大小和位置.


    //创建金币的背景图片
    for(int i = 0 ; i < 4;i++)
    {
        for(int j = 0 ; j < 4; j++)
        {
            //绘制背景图片
            QPixmap pix;
            pix.load(":/res/BoardNode.png");

            QLabel* label = new QLabel;
            label->setGeometry(0,0,50,50);
            label->setPixmap(pix);
            label->setParent(this);
            label->move(57 + i*pix.width(),200+j*pix.height());

            //金币对象
//            MyCoin * coin = new MyCoin(":/res/Coin0001.png");
//            coin->setParent(this);
//            coin->move(59 + i*pix.width(),204+j*pix.height());
        }
    }

    int gameArray[4][4]; //二维数组数据
    //初始化二维数组
    dataConfig config;

    for(int i = 0 ; i < 4;i++)
    {
        for(int j = 0 ; j < 4; j++)
        {
            gameArray[i][j] = config.mData[this->levalIndex][i][j];

            //金币对象
            QString img;
            if(gameArray[i][j] == 1)
            {
                img = ":/res/Coin0001.png";
            }
            else
            {
                img = ":/res/Coin0008.png";
            }
            //创建金币
            MyCoin * coin = new MyCoin(img);
            coin->setParent(this);
            coin->move(59 + i*50,204+j*50);
            //给金币属性赋值
            coin->posX = i; //记录x坐标
            coin->posY = j; //记录y坐标
            coin->flag =gameArray[i][j]; //记录正反标志

            coin->coinBtn[i][j] = coin;

            //监听点击信号按钮
            connect(coin,&MyCoin::clicked,[=](){
                //qDebug() << "点击的位置： x = " <<  coin->posX << " y = " << coin->posY ;
                coin->changeFlag();
                //gameArray[i][j] = (gameArray[i][j] == 0 ? 1 : 0); //数组内部记录的标志同步修改

                //判断是否胜利
//                this->isWin = true;
//                for(int i = 0 ; i < 4;i++)
//                {
//                    for(int j = 0 ; j < 4; j++)
//                    {
//                        //qDebug() << coinBtn[i][j]->flag ;
//                        if( coin->coinBtn[i][j]->flag == false)
//                        {
//                            this->isWin = false;
//                            break;
//                        }
//                    }
//                }

            });
        }
    }






}

void PlayScene::paintEvent(QPaintEvent *)
{
    //加载背景
    QPainter painter(this);
    QPixmap pix;
    pix.load(":/res/PlayLevelSceneBg.png");
    painter.drawPixmap(0,0,this->width(),this->height(),pix);

    //加载标题
    pix.load(":/res/Title.png");
    pix = pix.scaled(pix.width()*0.5,pix.height()*0.5);
    painter.drawPixmap( 10,30,pix.width(),pix.height(),pix);
}
